/*
 * Copyright (c) 2021. Moses All Right Reserved
 */

package com.moses.design_patterns.chain;

/**
 * @author Moses
 * @email shenhuohui@gmail.com
 * @date 2021/4/17 21:31
 */
// 责任链的核心类，请求处理的派发
public abstract class Handler {
    private Handler nextHandler = null;

    public final Response handle(Request request){
        Response response = null;
        // 判断当前 Handler 是否能进行处理请求
        // 不能请求则交由下一个 Handler
        if(getLevel() == request.getLevel()){
            response = this.response(request);
        }else {
            if(nextHandler != null){
                response =  nextHandler.handle(request);
            }else {
                System.out.println("该请求无法处理。。。");
            }
        }
        return response;
    }

    public void setNextHandler(Handler nextHandler) {
        this.nextHandler = nextHandler;
    }

    // 由实现类决定能处理什么级别的请求
    public abstract Level getLevel();

    // 由实现类类决定怎么处理请求
    public abstract Response response(Request request);
}
